import { getSignAsync, getAuthorizeUrl, getUserByCode } from "../api/wechat";
import config from "../config";
import qs from "querystring"
export const signture = async (ctx, next) => {
  let url = "http://173gg43187.iok.la";
  if (!url) {
    ctx.throw(404);
  }
  url = decodeURIComponent(url);
  let params = await getSignAsync(url);
  ctx.body = {
    success: 1,
    params
  };
};

export const redirect = async (ctx, next) => {
  let redirect = config.SITE_ROOT_URL + "/oauth";
  let scope = "snsapi_userinfo";
  const { visit, id } = ctx.query;
  const params = id ? `${visit}_${id}` : visit
  const url =await getAuthorizeUrl(redirect,params,scope);
  ctx.redirect(url);
};

export const oauth = async (ctx, next) => {
    const {code,state} = ctx.query
    const userInfo = await getUserByCode(code);
    ctx.body={
        success:1,
        usre:userInfo
    }
};
